Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core: make operations promise friendly #1689

Merged
merged 5 commits into from
Oct 14, 2016
Merged

core: make operations promise friendly #1689

merged 5 commits into from
Oct 14, 2016

Conversation

callmehiphop
Copy link
Contributor

@callmehiphop callmehiphop commented Oct 14, 2016

This makes operations Promise friendly. By default they are still EventEmitter instances, but now include a .promise() method as a convenience.

bigquery.startQuery(query)
  .then(function(data) {
    var job = data[0];
    return job.promise();
  })
  .then(function(data) {
    var metadata = data[0];
  }, function(err) {
    // Something went wrong!
  });

With Bluebird

bigquery.startQuery(query)
  .spread(function(job) {
    return job.promise();
  })
  .spread(function(metadata) {
    // yay
  });

In Node >= 6.0:

bigquery.startQuery(query)
  .then([job] => job.promise())
  .then([metadata] => {}, err => {});

@googlebot googlebot added the cla: yes This human has signed the Contributor License Agreement. label Oct 14, 2016
*
* @return {promise}
*/
GrpcOperation.prototype.promise = function() {

This comment was marked as spam.

This comment was marked as spam.

* // An error occurred during the job.
* });
*/
Job.prototype.promise = function() {

This comment was marked as spam.

This comment was marked as spam.

@stephenplusplus
Copy link
Contributor

Idea and code lgtm.

Job.prototype.promise = function() {
var self = this;

return new common.util.Promise(function(resolve, reject) {

This comment was marked as spam.

This comment was marked as spam.

@callmehiphop
Copy link
Contributor Author

@stephenplusplus tests have been added, PTAL!

var self = this;

return new self.Promise(function(resolve, reject) {
self.on('error', reject)

This comment was marked as spam.

This comment was marked as spam.

var self = this;

return new self.Promise(function(resolve, reject) {
self.on('error', reject)

This comment was marked as spam.

This comment was marked as spam.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes This human has signed the Contributor License Agreement. core
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants